package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRabbitmqOrg;
import com.pubinfo.passbook.common.entity.TRabbitmqOrgConvert;
import com.pubinfo.passbook.common.entity.TRabbitmqOrgPersonMerge;
import com.pubinfo.passbook.common.mapper.TRabbitmqOrgConvertMapper;
import com.pubinfo.passbook.common.mapper.TRabbitmqOrgPersonMergeMapper;
import com.pubinfo.passbook.common.model.bo.rabbitmq.RabbitMqRoot;
import com.pubinfo.passbook.common.service.master.TRabbitmqOrgConvertService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhouch
 * @date 2021/10/27 14:57
 */
@Service
public class TRabbitmqOrgConvertServiceImpl
    extends ServiceImpl<TRabbitmqOrgConvertMapper, TRabbitmqOrgConvert>
    implements TRabbitmqOrgConvertService {
  @Resource
  TRabbitmqOrgPersonMergeMapper orgPersonMergeMapper;
  
  @Override
  public Boolean processOrgMsg(RabbitMqRoot orgMsg) {
    String handleType = orgMsg.getHandle();
    TRabbitmqOrgConvert org = new TRabbitmqOrgConvert(new TRabbitmqOrg(orgMsg));
    
    // 检查是否存在
    boolean hasExist = new LambdaQueryChainWrapper<>(baseMapper)
        .eq(TRabbitmqOrgConvert::getOrgId, org.getOrgId())
        .count() > 0;
    
    switch (handleType) {
      case "insert":
      case "update":
        // 插入或者更新
        if (hasExist) {
          baseMapper.updateById(org);
        } else {
          baseMapper.insert(org);
        }
        break;
      
      case "delete":
        baseMapper.deleteById(org.getOrgId());
        
        // 删除关联表中所有关联该组织的关系
        new LambdaUpdateChainWrapper<>(orgPersonMergeMapper)
            .eq(TRabbitmqOrgPersonMerge::getOrgId, org.getOrgId())
            .remove();
        break;
      default:
        return false;
    }
    
    return true;
  }
  
  @Override
  public List<TRabbitmqOrgConvert> getSubOrgsBySuperOrgId(Long superOrgId) {
    return new LambdaQueryChainWrapper<>(baseMapper)
        .eq(TRabbitmqOrgConvert::getSupOrgId, superOrgId)
        .list();
  }
  
  @Override
  public TRabbitmqOrgConvert getOrgTreeRoot() {
    return baseMapper.getOrgTreeRoot();
  }
  
  @Override
  public List<TRabbitmqOrgConvert> searchOrgsByKeyword(String keyword) {
    return new LambdaQueryChainWrapper<>(baseMapper)
        .like(StringUtils.isNotBlank(keyword), TRabbitmqOrgConvert::getOrgName, keyword.trim())
        .list();
  }
  
  @Override
  public TRabbitmqOrgConvert getOrgWithSuperRelation(Long orgId) {
    TRabbitmqOrgConvert currentOrg = baseMapper.selectById(orgId);
  
    /**
     * 一直检索当前组织的父节点...直到找不到父节点ID, 最后组装成一个节点树.
     */
    for (; ; ) {
      if (currentOrg.getSupOrgId() == null || StringUtils.isBlank(currentOrg.getSupOrgId())) {
        break;
      }
      TRabbitmqOrgConvert supOrg = baseMapper.selectById(currentOrg.getSupOrgId());
      supOrg.setSubOrgs(Arrays.asList(currentOrg));
      currentOrg = supOrg;
    }
    
    return currentOrg;
  }
  
}
